﻿@using static CnGalWebSite.Components.Inputs.MasaTextSelect;

<div class="row">
    <div class="col-12 col-lg-6">
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="对比的版本" Icon="@IconType.Key.ToIconString()" Outline>
            <ButtonContent>
                <div style="width:220px">
                    <CnGalWebSite.Components.Inputs.MasaTextSelect Items="ContrastItems" ValueChanged="OnContrastChanged" Value="ContrastItem" HideDetails="true"/>
                </div>
            </ButtonContent>
            <ChildContent>
                @ContrastView
            </ChildContent>
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    </div>
    <div class="col-12 col-lg-6">
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="当前的版本" Icon="@IconType.Lock.ToIconString()" Outline>
            <ButtonContent>
                <div style="width:220px">
                    <CnGalWebSite.Components.Inputs.MasaTextSelect Items="CurrentItems" ValueChanged="OnCurrentChanged" Value="CurrentItem" HideDetails="true" />
                </div>
            </ButtonContent>
            <ChildContent>
                @CurrentView
            </ChildContent>
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    </div>
</div>




@code {
    [Parameter]
    public ExaminesOverviewViewModel Model { get; set; }
    [Parameter]
    public RenderFragment ContrastView { get; set; }
    [Parameter]
    public RenderFragment CurrentView { get; set; }

    [Parameter]
    public EventCallback<string> ContrastChanged { get; set; }
    [Parameter]
    public EventCallback<string> CurrentChanged { get; set; }

    private List<SelectedItem> ContrastItems { get; set; } = new List<SelectedItem>();
    private List<SelectedItem> CurrentItems { get; set; } = new List<SelectedItem>();
    [Parameter]
    public string ContrastItem { get; set; }
    [Parameter]
    public string CurrentItem { get; set; }

    bool isContrastFirst = true;
    bool isCurrentFirst = true;


    protected override void OnInitialized()
    {
        GenerateList();
    }

    public void GenerateList()
    {
        ContrastItems.Clear();
        CurrentItems.Clear();
        var isContrast = false;
        var isCurrent = false;
        foreach (var item in Model.Examines)
        {
            if (isCurrent == false)
            {
                ContrastItems.Add(new SelectedItem
                    {
                        Text = $"『{item.Id.ToString("00000")}』 {item.ApplyTime.ToString("yyyy/M/d HH:mm")}",
                        Value = item.Id.ToString()
                    });
                if (item.IsSelected)
                {
                    ContrastItem = item.Id.ToString();
                }
            }
            if (item.Id.ToString() == CurrentItem)
            {
                isCurrent = true;
            }
            if (item.IsSelected)
            {
                isContrast = true;
            }
            if (isContrast)
            {
                CurrentItems.Add(new SelectedItem
                    {
                        Text = $"『{item.Id.ToString("00000")}』 {item.ApplyTime.ToString("yyyy/M/d HH:mm")}",
                        Value = item.Id.ToString()
                    });
                if (CurrentItem == item.Id.ToString())
                {
                    CurrentItem = item.Id.ToString();
                }
            }
        }
        if (CurrentItems.Count == 0)
        {
            var item = Model.Examines.Last();
            CurrentItems.Add(new SelectedItem
                {
                    Text = $"『{item.Id.ToString("00000")}』 {item.ApplyTime.ToString("yyyy/M/d HH:mm")}",
                    Value = item.Id.ToString()
                });
            if (CurrentItem == item.Id.ToString())
            {
                CurrentItem = item.Id.ToString();
            }
        }

    }

    public async Task OnContrastChanged(string value)
    {
        if (isContrastFirst)
        {
            isContrastFirst = false;
            return;
        }
        foreach (var temp in Model.Examines)
        {
            if (temp.Id.ToString() == value)
            {
                temp.IsSelected = true;
            }
            else
            {
                temp.IsSelected = false;
            }
        }

        GenerateList();
        StateHasChanged();
        await ContrastChanged.InvokeAsync(value);
    }
    public async Task OnCurrentChanged(string value)
    {
        if (isCurrentFirst)
        {
            isCurrentFirst = false;
            return;
        }
        GenerateList();
        StateHasChanged();
        await CurrentChanged.InvokeAsync(value);
    }
}
